package cn.coderliu.controller;

import cn.coderliu.config.RedisUtil;
import cn.coderliu.entty.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author liu
 */
@RestController
public class TestController {

    @Autowired
    RedisUtil redisUtil;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    //@Lock
    @GetMapping(value = "/test")
    public Object test(User user) {
        Boolean lock = redisUtil.lock("test", 5);
        if (lock) {
            System.out.println(new Date());
        }

        if (redisUtil.get("abc") != null) {
            Integer a = Integer.valueOf(redisUtil.get("abc").toString());

            Boolean lock100 = false;
            if (a < 10) {
                lock100 = redisUtil.lock("test");
            }
            if (lock100) {
                Long abc = redisTemplate.opsForValue().increment("abc", 1L);
                System.out.println(abc);
            }

        } else {
            redisUtil.set("abc", 0);
        }
        //  redisUtil.del("test");
        return null;
    }

}
